package dao.impl;

import dao.UserDao;
import db.DbConnection;
import pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    PreparedStatement ps = null;
    ResultSet rs = null;
    private Connection conn = null;

    @Override
    public User search(String number) {
        conn = DbConnection.getConnection();
        User user=null;
        try {
            String sql = "select * from user where userId=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, number);
            rs = ps.executeQuery();
            while (rs.next()) {
                String userId = rs.getString(1);
                String userName = rs.getString(2);
                String passWord = rs.getString(3);
                String gender = rs.getString(4);
                String phoneNumber = rs.getString(5);
                String type = rs.getString(6);
                user = new User(userId, userName, passWord, gender, phoneNumber, type);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(ps, rs, conn);
        }
        return user;
    }

    @Override
    public void delete(String userId) {
        Connection conn = DbConnection.getConnection();
        try {
            String sql = "delete from user where userId=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, userId);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(ps, rs,conn);
        }
    }

    @Override
    public List<User> searchAll() {
        conn = DbConnection.getConnection();
        List<User> userList = new ArrayList<User>();
        try {
            String sql = "select * from user ";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                String userId = rs.getString(1);
                String userName = rs.getString(2);
                String passWord = rs.getString(3);
                String gender = rs.getString(4);
                String phoneNumber = rs.getString(5);
                String type = rs.getString(6);
                User user = new User(userId, userName, passWord, gender, phoneNumber, type);
                userList.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(ps, rs, conn);
        }
        return userList;
    }

    @Override
    public void add(User user) {
        conn = DbConnection.getConnection();
        try {
            String sql = "insert into user values(?,?,?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUserId());
            ps.setString(2, user.getUserName());
            ps.setString(3, user.getPassWord());
            ps.setString(4, user.getGender());
            ps.setString(5, user.getPhoneNumber());
            ps.setString(6, user.getType());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DbConnection.close(ps,rs,conn);
        }
    }

        @Override
    public void update(User user) {
        conn = DbConnection.getConnection();
        try {
            String sql = "update user set userName=?,passWord=?,gender=?,phoneNumber=?,type=? where userId=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUserName());
            ps.setString(2, user.getPassWord());
            ps.setString(3, user.getGender());
            ps.setString(4, user.getPhoneNumber());
            ps.setString(5, user.getType());
            ps.setString(6, user.getUserId());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(ps, rs,conn);
        }

    }
}


